package com.hadluo.qq.server.service.impl;

import com.hadluo.qq.server.entity.GroupMessage;
import com.hadluo.qq.server.mapper.GroupMessageMapper;
import com.hadluo.qq.server.service.GroupMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class GroupMessageServiceImpl implements GroupMessageService {

    @Autowired
    private GroupMessageMapper groupMessageMapper;

    @Override
    public GroupMessage saveGroupMessage(GroupMessage message) {
        if (message.getStatus() == null) {
            message.setStatus(0); // 默认未发送状态
        }
        message.setCreatedAt(new Date());
        message.setUpdatedAt(new Date());
        groupMessageMapper.insert(message);
        return message;
    }

    @Override
    public GroupMessage getGroupMessageById(Long id) {
        return groupMessageMapper.selectById(id);
    }

    @Override
    public List<GroupMessage> getGroupMessages(Long groupId, int page, int size) {
        int offset = (page - 1) * size;
        return groupMessageMapper.selectByGroupId(groupId, offset, size);
    }

    @Override
    public boolean updateMessageStatus(Long messageId, Integer status) {
        return groupMessageMapper.updateStatus(messageId, status) > 0;
    }

    @Override
    public boolean deleteMessage(Long messageId) {
        return groupMessageMapper.deleteById(messageId) > 0;
    }

    @Override
    public int getGroupMessageCount(Long groupId) {
        return groupMessageMapper.countByGroupId(groupId);
    }

    @Override
    public List<GroupMessage> getNewMessages(Long groupId, Date timestamp) {
        return groupMessageMapper.selectNewMessages(groupId, timestamp);
    }
}